package com.mlnx.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mlnx.common.utils.MyLog;
import com.mlnx.data.entity.BpInfo;
import com.mlnx.device.config.PoolConfig;
import com.mlnx.device.entity.Device;
import com.mlnx.device.pojo.dto.BpCache;
import com.mlnx.device.pojo.dto.EcgDeviceDto;
import com.mlnx.device.service.CallBack;
import com.mlnx.device.service.DeviceNettyService;
import com.mlnx.device.service.DeviceService;
import com.mlnx.device.service.EcgDeviceService;

import org.shan.spring.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import io.netty.channel.Channel;

/**
 * @author zzb
 * @create 2019/10/23 14:25
 */
@Service
public class DeviceNettyServiceImpl implements DeviceNettyService {

    private MyLog log = MyLog.getLog(getClass());

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private EcgDeviceService ecgDeviceService;

    @Override
    public void getEcgDevice(String deviceId, CallBack<EcgDeviceDto> callBack) {

        PoolConfig.deviceNettyExecutorService.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    EcgDeviceDto ecgDeviceDto = ecgDeviceService.getEcgDeviceDto(deviceId);
                    callBack.onBack(ecgDeviceDto);
                }catch (Exception e){
                    log.error(e, "获取心电设备信息异常");
                    callBack.error("获取心电设备:"+deviceId+" 异常");
                }
            }
        });
    }

    @Override
    public void getPatientId(String deviceId, CallBack<Integer> callBack) {

        PoolConfig.deviceNettyExecutorService.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    Device device = deviceService.getOne(new QueryWrapper<Device>().eq("numeration", deviceId));
                    if (device == null && device.getPatientId() == null || device.getPatientId() == 0) {
                        callBack.error("设备:" + deviceId + " 未绑定患者");
                    } else {
                        callBack.onBack(device.getPatientId());
                    }
                }catch (Exception e){
                    log.error(e, "获取设备信息异常");
                    callBack.error("获取设备:"+deviceId+" 异常");
                }
            }
        });
    }

    @Override
    public Long getBpLastTime(String deviceId) {

        return (Long) redisUtil.get(deviceId);
    }

    @Override
    public void setBpLastTime(String deviceId, Long time) {
        redisUtil.set(deviceId, time);
    }

    @Override
    public void setXmbpCache(Channel channel, BpInfo bpInfo) {
                BpCache.getInstance().addBpinfo(channel, bpInfo);
    }

    @Override
    public List<BpInfo> getXmbpCache(Channel channel) {

        return  BpCache.getInstance().getBpList(channel);
    }

    @Override
    public Device getDevice(String deviceId) {
        return  deviceService.getDevice(deviceId);
    }

}
